/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jav.util;

import com.sun.lwuit.Component;
import com.sun.lwuit.List;
import com.sun.lwuit.list.DefaultListCellRenderer;
import com.sun.lwuit.plaf.UIManager;

/**
 *
 * @author Javier
 */
public class TickerRenderer extends DefaultListCellRenderer {

    private DefaultListCellRenderer selectedRenderer = new DefaultListCellRenderer(false);
    private List parentList;

    public TickerRenderer() {
        super(false);
    }

    public boolean animate() {
        if (parentList != null && parentList.getComponentForm() != null) {
            if (selectedRenderer.isTickerRunning()) {
                if (selectedRenderer.animate()) {
                    parentList.repaint();
                }
            }
        }
        return super.animate();
    }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
        if (isSelected) {
            selectedRenderer.getListCellRendererComponent(list, value, index,
                    isSelected);

            // sometimes the list asks for a dummy selected value for size
            // calculations and this might break the tickering state
            if (index == list.getSelectedIndex()) {
                if (selectedRenderer.shouldTickerStart()) {
                    if (!selectedRenderer.isTickerRunning()) {
                        parentList = list;
                        list.getComponentForm().registerAnimated(this);
                        selectedRenderer.startTicker(UIManager.getInstance().
                                getLookAndFeel().getTickerSpeed(), true);
                    }
                } else {
                    if (selectedRenderer.isTickerRunning()) {
                        selectedRenderer.stopTicker();
                    }
                }
            }
            return selectedRenderer;
        } else {
            return super.getListCellRendererComponent(list, value, index, isSelected);
        }
    }
}
